package com.example.webcomen.Handler;

import cn.hutool.core.util.ObjectUtil;
import com.example.webcomen.Service.LoginService;
import com.example.webcomen.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    LoginService loginService;

    @Autowired
    UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if(ObjectUtil.isEmpty(userService.getByUserName(username))){
            throw new UsernameNotFoundException("用户名或密码错误");
        }
        User user=new User(username,userService.getByUserName(username).getPassword(),loginService.authorityList());
        return user;
    }
}
